package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.teng.smartdrawer.R;
import com.teng.smartdrawer.bean.History;

import java.util.List;

/**
 * Created by teng on 16/5/13.
 */
public class HistoryAdapter extends BaseAdapter{

    private List<History> historyList;

    private Context context;


    public HistoryAdapter(List<History> historyList, Context context) {
        this.historyList = historyList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return historyList == null ? 0 : historyList.size();
    }

    @Override
    public Object getItem(int position) {
        return historyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_history , parent , false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();

        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.component_name = (TextView) ret.findViewById(R.id.history_component_name);
            viewHolder.history_number = (TextView) ret.findViewById(R.id.history_number);
            viewHolder.history_time = (TextView) ret.findViewById(R.id.history_time);
            viewHolder.user_name = (TextView) ret.findViewById(R.id.history_user_name);
//            viewHolder.option = (ImageView) ret.findViewById(R.id.option);
        }

        History history = historyList.get(position);

        viewHolder.history_time.setText(history.getH_time());

        String h_name = history.getH_name();

        if (h_name == null || h_name.equals(""))
            h_name = "电容假数据";

        viewHolder.component_name.setText(h_name);

        viewHolder.user_name.setText(history.getH_user());

        viewHolder.history_number.setText(history.getH_number());

        String h_number = history.getH_number();

        if ("in".equals(history.getH_operation())){

            viewHolder.history_number.setTextColor(Color.BLUE);
            viewHolder.history_number.setText("+"+ h_number);

        }else  if ("out".equals(history.getH_operation())){

            viewHolder.history_number.setTextColor(Color.RED);
            viewHolder.history_number.setText("-"+ h_number);

        }else  if ("del".equals(history.getH_operation())){

            viewHolder.history_number.setTextColor(Color.GREEN);
            viewHolder.history_number.setText("del");

        }

        return ret;
    }



    private class ViewHolder{
        public TextView history_time;
        public TextView component_name , user_name;
//        public ImageView option;
        public TextView history_number;
    }

}
